<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace App\Model;
use App\Entity\ApiMoney;
use App\Base\BaseModel;
use App\Setting\ApiMoneySetting;

class ApiMoneyModel extends BaseModel{

    /*
     * 列表基本设置【通用模块必须开发】
     */
    public function listSettingDefault()
    {
        $settings = new ApiMoneySetting($this->getDoctrine());
        return $settings->listConfig();
    }

    /*
     * 为后台获取所有文章分类【通用模块必须开发】
     */
    /**
     * @param array $params $param[textKey]=textKey , $param[conditions][table][field][eq|gt|lt...]=value , $param[orderBy][table][field]=DESC|ASC
     * @param int $offset
     * @param int $perPage
     * @return array
     */
    public function getAdminLists($params ,$offset = 0 , $perPage = 10)
    {
        $qb = $this->getQueryFromParam($params);
        $lists = $this->getAllResult($qb,$offset,$perPage);
        $total = $this->countAllResult($qb);

        return array(
            "total" => $total ? $total : 0,
            "data"  => $lists
        );
    }

    /**
     * @return \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
     */
    public function getRep()
    {
        $settings = new ApiMoneySetting($this->getDoctrine());
        return $this->em->getRepository($settings->bundleName.":".$settings->entityName);
    }

    /**
     * @return ApiMoney[]
     */
    public function getAll()
    {
        return $this->getRep()->findBy(array(),array("id"=>"DESC"));
    }

    /**
     * @param $id
     * @return null|object|ApiMoney
     */
    public function getById($id)
    {
        return $this->getRep()->find($id);
    }

    /**
     * @return array
     */
    public function getAllArray()
    {
        $data = $this->getAll();
        $re = [];
        foreach ($data as $one) {
            $re[] = $this->toArray($one);
        }
        return $re;
    }

    /**
     * @param $id
     * @return array
     */
    public function getByIdArray($id)
    {
        $en = $this->getById($id);
        return $en?$this->toArray($en):[];
    }

    /**
     * @param $project
     * @param $user
     * @param $type
     * @return null|object|ApiMoney
     */
    public function getMoney($project,$user,$type)
    {
        $userMoney =  $this->getRep()->findOneBy(array(
            "projectId"=>$project,
            "userId"=>$user,
            "type"=>$type,
        ));
        if (!$userMoney) {
            $userMoney = new ApiMoney();
            $userMoney->setProjectId($project)
                ->setUserId($user)
                ->setMoney(0)
                ->setType($type);
            $this->em->persist($userMoney);$this->em->flush();
        }
        return $userMoney;
    }

    /**
     * @param $project
     * @param $user
     * @return array
     */
    public function getMoneyProject($project,$user)
    {
        $ApiMoneyTypeModel = new ApiMoneyTypeModel($this->getDoctrine());
        $types = $ApiMoneyTypeModel->getByProject($project);
        $re = [];
        foreach ($types as $type) {
            $name = $type->getName();
            $id = $type->getId();
            $data = [];
            $data["id"] = $id;
            $data["name"] = $name;
            $data["money"]= $this->getMoney($project,$user,$id)->getMoney();
            $re[] = $data;
        }
        return $re;
    }

    public function addMoney($project,$user,$moneyType,$money)
    {
        $userMoney = $this->getMoney($project,$user,$moneyType);
        $num = $userMoney->getMoney() + $money;
        if ($num < 0) {
            return "余额不足";
        }
        $userMoney->setMoney($num);
        $this->em->persist($userMoney);$this->em->flush();
        return false;
    }


}